package com.zhijie.common.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JsonUtils {
	/**
	 * 线程安全
	 */
	private static final ObjectMapper MAPPER = new ObjectMapper()
			.setSerializationInclusion(JsonInclude.Include.NON_NULL);

	public static String objToString(Object object) {
		Assert.notNull(object, "object cannot be null");
		try {
			return MAPPER.writeValueAsString(object);
		} catch (JsonProcessingException e) {
			throw new RuntimeException(e);
		}
	}

	public static <T> T stringToObj(String json, Class<T> clazz) {
		Assert.isTrue(!StringUtils.isEmpty(json), "json cannot be empty");
		Assert.notNull(clazz, "clazz cannot be null");
		try {
			return MAPPER.readValue(json, clazz);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static <T> List<T> stringToList(String json, Class<T> clazz) {
		try {
			JavaType javaType = MAPPER.getTypeFactory().constructParametricType(ArrayList.class, clazz);
			List<T> list = MAPPER.readValue(json, javaType);
			return list;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
